//
//  WCNSwitchItemView.m
//  TwoControllerInPage
//
//  Created by wsj2015 on 15/10/31.
//  Copyright (c) 2015年 wsj2015. All rights reserved.
//

#import "WCNSwitchItemView.h"

#define kBtn_interSapce 30
@implementation WCNSwitchItemView
{
    UIView *_bgView;
    UIButton *_itemOne;
    UIButton *_itemTwo;
    
    UILabel *_titleLabel;
    NSInteger _selectIitem;
    
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        
        _bgView =[[UIView alloc]initWithFrame:frame];
        _bgView.backgroundColor =[UIColor whiteColor];
        [self addSubview:_bgView];
        NSArray *titleArray = @[@"资讯",@"护理案例"];
        CGFloat btnW = (frame.size.width - kBtn_interSapce) /2;
        CGFloat btnH =  30;
        for (int i = 0; i <2; i++) {
            UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(i *(btnW + kBtn_interSapce/2), 0, btnW, btnH);
            [btn setTitle:titleArray[i] forState:UIControlStateNormal];
            [btn setTitle:@"" forState:UIControlStateSelected];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            btn.layer.borderWidth =1;
            btn.layer.borderColor = [UIColor redColor].CGColor;
            btn.layer.cornerRadius = 10;
            btn.layer.masksToBounds = YES;
            [btn addTarget:self action:@selector(changeItemAtion:) forControlEvents:UIControlEventTouchUpInside];
            [_bgView addSubview:btn];
            btn.tag = 100 +i;
            if (i == 0) {
                _selectIitem = 0;
                btn.selected = YES;
                _itemOne =btn;
                
                UILabel *titleLabel =[[UILabel alloc]initWithFrame:_itemOne.frame];
                titleLabel.text =@"资讯";
                titleLabel.layer.cornerRadius = 10;
                titleLabel.layer.masksToBounds = YES;
                titleLabel.backgroundColor =[UIColor orangeColor];
                titleLabel.textColor = [UIColor redColor];
                titleLabel.textAlignment = NSTextAlignmentCenter;
                _titleLabel =titleLabel;
                [_bgView addSubview:_titleLabel];
                
            }else{
                _itemTwo =btn;
            }
            
        }
   
    }
    return self;

}
- (void)changeItemAtion:(UIButton *)btn
{
    UIButton *preBtn = (UIButton *)[_bgView viewWithTag:100 + _selectIitem];
    if (preBtn == btn) {
        return ;
    }
    preBtn.selected = NO;
    
    btn.selected = YES;
    _selectIitem = btn.tag  - 100;
    if (self.changeItemBlock) {
        self.changeItemBlock(_selectIitem);
    }
    if (_selectIitem == 0) {
        [self moveAnimationView:YES];
    }else{
        [self moveAnimationView:NO];
    }
    
 
}
- (void)moveAnimationView:(BOOL)isLeft
{
    CGRect frame;
    if (isLeft) {
        frame = _itemOne.frame;
        _titleLabel.text =@"资讯";
    }else{
        frame =_itemTwo.frame;
        _titleLabel.text =@"护理案例";
    }
    [UIView animateWithDuration:0.35f delay:0 usingSpringWithDamping:.6f initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        _titleLabel.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
   
}
@end
